import PICKER from "../picker/picker.vue"

let $vmPicker;
let data1 = [
  {
    text: '输出',
    value: 'a'
  }, {
    text: '控制',
    value: 'b'
  },
  {
    text: '核心',
    value: 'c'
  },
  {
    text: '爆发',
    value: 'd'
  },
  {
    text: '辅助',
    value: 'e'
  },
  {
    text: '打野',
    value: 'f'
  },
  {
    text: '逃生',
    value: 'g'
  }, {
    text: '先手',
    value: 'h'
  }
]
let data2 = [
  {
    text: '输出',
    value: 'a'
  }, {
    text: '控制',
    value: 'b'
  },
  {
    text: '核心',
    value: 'c'
  },
  {
    text: '爆发',
    value: 'd'
  },
  {
    text: '辅助',
    value: 'e'
  },
  {
    text: '打野',
    value: 'f'
  },
  {
    text: '逃生',
    value: 'g'
  }, {
    text: '先手',
    value: 'h'
  }
]
let data = [data1, data2]
export const pickerY = {
  install(Vue){
    const timePicker = Vue.extend(PICKER)

    if (!$vmPicker) {
      $vmPicker = new timePicker({
        el: document.createElement('div')
      })
      document.body.appendChild($vmPicker.$el)
    }

    Vue.prototype._pickerY = (options) => {
      options = options || {};

      //按钮文字
      $vmPicker.confirmTxt = options.confirmTxt || "确认";
      $vmPicker.cancelTxt = options.cancelTxt || "取消";

      $vmPicker.$off('select');
      $vmPicker.$off('change');
      //日期选择
      if (options.type) {
        //滚动位置改变索引 用于联动滚动
        let val = dateGet.getDateArr(undefined, undefined, options.type);
        $vmPicker.data = val.data;
        $vmPicker.setSelectedIndex(val.index);

        $vmPicker.$on('change', (value, index, wheels) => {
          if (value < 2) {
            let m = $vmPicker.data[1][wheels[1].getSelectedIndex()].value;
            if (m === 2 || value === 1) {
              let y = $vmPicker.data[0][wheels[0].getSelectedIndex()].value;
              $vmPicker.data = dateGet.getDateArr(y, m, options.type).data;
              setTimeout(() => {
                wheels[2].refresh();
              }, 120);
            }
          }
        });
      }
      //默认选择
      else {
        $vmPicker.data = options.data;
        //设置初始滚动位置[索引0，索引1]
        if (options.selectedIndex) {
          $vmPicker.setSelectedIndex(options.selectedIndex);
        } else {
          let selectedIndexDefault = [];
          for (let i = 0; i < $vmPicker.data.length; i++) {
            selectedIndexDefault.push(0);
          }
          $vmPicker.setSelectedIndex(selectedIndexDefault);
        }
      }

      //选中值
      $vmPicker.$on('select', (value, index, text) => {
        let d = {};
        if (options.type) {
          if (options.type === 3) {
            d.timeText = text[0] + "-" + text [1] + "-" + text[2] + " " + text[3] + ":" + text[4];
            d.timeValue = new Date(value[0], value[1] - 1, value[2], value[3], value[4]);
          } else {
            d.timeText = text.join("-");
            d.timeValue = new Date(value[0], value[1] - 1, value[2]);
          }
        }else {
          d = {value, index, text};
        }

        options.onConfirm && options.onConfirm(d);
      });

      $vmPicker.show();
    }

    let dateGet = {
      getDateArr(year, month, flag){
        let date = new Date();
        let currentYear = date.getFullYear();
        let currentMonth = date.getMonth() + 1;
        let currentDay = date.getDate();
        let indexFlag = false;
        if (year === undefined) {
          indexFlag = true;
        }
        year = year || currentYear;
        month = month || currentMonth;
        let startYear = 1960;
        let endYear = 2060;
        let yearList = [];
        let monthList = [];
        for (let i = startYear; i <= endYear; i++) {
          yearList.push({value: i, text: i});
        }
        for (let i = 1; i <= 12; i++) {
          monthList.push({value: i, text: this.format(i)});
        }
        let dayList = this.getDays(year, month);

        let dataList = [yearList, monthList, dayList];
        let scrollIndex;
        let timeList;
        if (flag === 3) {
          timeList = this.getTimeArr();
          dataList = dataList.concat(timeList);
        }
        if (indexFlag) {
          scrollIndex = [];
          scrollIndex.push(this.getIndex(yearList, currentYear));
          scrollIndex.push(this.getIndex(monthList, currentMonth));
          scrollIndex.push(this.getIndex(dayList, currentDay));
          if (flag === 3) {
            let currentHour = date.getHours();
            let currentMinutes = date.getMinutes();
            scrollIndex.push(this.getIndex(timeList[0], currentHour));
            scrollIndex.push(this.getIndex(timeList[1], currentMinutes));
          }
        }

        return {
          data: dataList,
          index: scrollIndex
        }
      },
      getDays(year, month){
        let dayLength = new Date(parseInt(year), parseInt(month), 0).getDate();
        let dayArr = [];
        for (let i = 1; i <= dayLength; i++) {
          dayArr.push({value: i, text: this.format(i)});
        }
        return dayArr;
      },
      getIndex(arr, v){
        return arr.findIndex((i) => {
          return parseInt(v) === i.value;
        })
      },
      getTimeArr(){
        let hourList = [];
        let minutesList = [];
        for (let i = 0; i <= 24; i++) {
          hourList.push({value: i, text: this.format(i)})
        }
        for (let i = 0; i <= 60; i++) {
          minutesList.push({value: i, text: this.format(i)})
        }
        return [hourList, minutesList]
      },
      format(v){
        return v < 10 ? "0" + v : v
      }
    }
  }
}
/*
 this._pickerY({
  data:[arr1,arr2],
  type:2 //2：年月日选择  3：年月日和时间 有type时不需要data
  selectedIndex:[0,1],//滚动到数组1的index 和 数组2的index
  onConfirm:(v)=>{
    //时间 v.timeText v.timeValue  默认 {value, index, text};
    console.log(v);
  }
 });
 */
